<?php

use \GatewayWorker\Lib\Gateway;

class reconnect extends cmdbase
{
	public function onCmd($msg){
		//组装玩家id
		$client_mgr = $GLOBALS['ClientMgr']; 
		$user_id = $client_mgr->getUid($msg['user'], $msg['psw']);

 		//把在线的旧玩家踢下线
		if(Gateway::isUidOnline($user_id)){
			$client_id_list = Gateway::getClientIdByUid($user_id);
			foreach ($client_id_list as $client_id) {
				$message = Events::wrapMsg("notify_kick", array('msg'=>"reconnect kick out"));
				Gateway::closeClient($client_id, $message);
				echo "warning=>玩家在线，需要提下线:$client_id\n";
			}
		}

		$client_id = $this->getCurClientId();
		//绑定用户id，方便下次踢人或者恢复现场
 		Gateway::bindUid($client_id, $user_id);

 		//如果还在房间，加入房间
		$group_mgr = $GLOBALS['GroupMgr']; 
		$group_id = $group_mgr->getActiveGroupId($user_id);
		if($group_id > 0){
			echo "加入旧组：$group_id\n";
			$group_mgr->joinGroup($client_id, $group_id);

			//广播
			$this->sendToGroup("notify_enterroom", array('uid'=>$user_id,'num' => Gateway::getClientCountByGroup($group_id)));

			//恢复现场
			$person_wrap = $group_mgr->getBattlePersonWrap($group_id);
			if(isset($person_wrap)){
				//发送信息
				$this->sendToCurrentClient("notify_syncbattle", $person_wrap);
			}
		}

 		$msg_body = array(
            'ret'=> NetErrorCode::$None,
            'reason'=>$user_id,
        );
 		return $msg_body;
	}
}